//
//  AccountViewController.m
//  CMVRFForIOS
//
//  Created by HailongHan on 15/4/19.
//  Copyright (c) 2015年 hvacapp. All rights reserved.
//

#import "AccountViewController.h"
#import <TPKeyboardAvoidingTableView.h>
#import "UserProfileData.h"
#import "Icon_Input_Cell.h"
#import "ProvinceData.h"
#import "SelectionNationViewController.h"
#import "SelectProvinceViewController.h"
#import "SelectCityViewController.h"
#import <MBProgressHUD.h>





@interface AccountViewController (){
    MBProgressHUD *HUD;
    BOOL passwordModified;
    BOOL parentCompanyModified;
    
}

@property (nonatomic,strong)UITableView *tableView;


@property (nonatomic,copy) NSString *mUserLevel;

//注册类型  暖通公司、经销商、销售人员
@property (nonatomic,assign) UserType mRegisterType;

/**
 *  用户账号
 */
@property (nonatomic,copy) NSString *mAccount;
/**
 *  用户密码
 */
@property (nonatomic,copy) NSString *mPassword;

/**
 *  用户确认密码
 */
@property (nonatomic,copy) NSString *mConfirmPassword;



/**
 ***************  暖通公司  *****************
 */

@property (nonatomic,copy) NSString *mCompanyFullName;
@property (nonatomic,copy) NSString *mCompanyContactName;
@property (nonatomic,copy) NSString *mCompanyContactPhone;
@property (nonatomic,copy) NSString *mCompanyCitys;

/**
 ***************  经销商  *****************
 */



/**
 ***************  经销商  *****************
 */


/**
 *  选择国家、省份、城市后，保存的数据，都可以多选
 */
@property (nonatomic,strong) NSMutableArray *selectedNation;
@property (nonatomic,strong) NSMutableArray *selectedProvince;
@property (nonatomic,strong) NSMutableArray *selectedCity;

@property (nonatomic,strong) UserProfileData *selectedAdministrator;

@property (nonatomic,strong) UserProfileData *selectedBranchCompany;
@property (nonatomic,copy) NSString *mBranchCompanyName;

@property (nonatomic,strong) UserProfileData *mUserProfileData;


@end

@implementation AccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initData];
    [self setupViews];
    [self loadUserProfileData];
}

#pragma mark - 初始化视图
- (void)setupViews{
    self.title = @"账号管理";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(finish)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"修改" style:UIBarButtonItemStyleDone target:self action:@selector(saveAccount)];
    
    self.tableView = ({
        TPKeyboardAvoidingTableView *tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:[UIView frameWithOutNav] style:UITableViewStyleGrouped];
        tableView.dataSource = self;
        tableView.delegate = self;
        [self.view addSubview:tableView];
        tableView;
    });
}

- (void)finish {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)saveAccount{
    
    if (self.mAccount == nil) {
        [Tools showMessageCenter:@"邮箱不能为空"];
        return;
    }
    
    if (passwordModified && self.mPassword == nil) {
        [Tools showMessageCenter:@"密码不能为空"];
        return;
    }
    
    if (passwordModified && self.mPassword.length < 4) {
        [Tools showMessageCenter:@"密码必须大于4位"];
        return;
        
    }
    
    if (passwordModified && ![self.mPassword isEqualToString:self.mConfirmPassword]) {
        [Tools showMessageCenter:@"两次密码不一致"];
        return;
    }
    
    if (self.mUserProfileData.contactName == nil || self.mUserProfileData.contactName.length == 0) {
        [Tools showMessageCenter:@"请输入联系人姓名"];
        return;
    }
    
    if (self.mUserProfileData.phoneNumber == nil || self.mUserProfileData.phoneNumber.length == 0) {
        [Tools showMessageCenter:@"请输入联系人手机号码"];
        return;
    }
    
    if (self.mRegisterType == UserTypeBranchCompany || self.mRegisterType == UserTypeDealer) {
        
        if (self.mUserProfileData.name == nil || self.selectedCity.count == 0) {
            [Tools showMessageCenter:@"请输入经营区域的城市"];
            return;
        }
    }
    
    if (self.mRegisterType == UserTypeBranchCompany && self.selectedAdministrator != nil) {
        self.mUserProfileData.parentCompanyUserName = self.selectedAdministrator.userName;
        self.mUserProfileData.parentCompany = self.selectedAdministrator.name;
    }
    else if (self.selectedBranchCompany != nil){
        self.mUserProfileData.parentCompanyUserName = self.selectedBranchCompany.userName;
        self.mUserProfileData.parentCompany = self.selectedBranchCompany.name;
    }
    
    self.mUserProfileData.userName = self.mAccount;
    
    //获得经营城市的字符串组合
    if (self.selectedCity != nil && self.selectedCity.count > 0) {
        NSMutableString *cityStr = [NSMutableString new];
        BOOL isFirst = YES;
        for (ProvinceData *item in self.selectedCity) {
            if (isFirst) {
                [cityStr appendString:item.guidId];
                isFirst = NO;
            }
            else {
                [cityStr appendFormat:@",%@",item.guidId];
            }
        }
        self.mUserProfileData.operationCities = cityStr;
    }
    
    
    //参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (passwordModified) {
        [params setValue:[self.mPassword base64Encoder] forKey:@"newPassword"];
    }
    else {
        [params setValue:[NSNull null] forKey:@"newPassword"];
    }
 
    NSDictionary *profile = [self.mUserProfileData keyValues];
    [params setObject:profile forKey:@"userProfile"];
    
    NSData *jsonData = [params JSONString];
    NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"json data:%@",json);
    
    [self showLoading];
    
    [HttpClient postWithUrl:HTTP_POST_USER_INFO token:[[AccountManager manager]token] paramDic:params
                resultClass:[CommonResponse class] success:^(CommonResponse *obj) {
                    [HUD hide:YES];
                    [UIAlertView bk_showAlertViewWithTitle:@"提示" message:@"保存用户信息成功！" cancelButtonTitle:@"确定" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                        if (buttonIndex == 0) {
                            [self finish];
                        }
                    }];
                } failure:^(NSError *error) {
                    [HUD hide:YES];
                    [Tools showMessageCenter:@"保存用户信息失败"];
                    CMLog(@"错误信息为：%@",error);
                }];
}

- (void)showLoading{
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.labelText = @"保存用户信息中...";
}

#pragma mark - 数据拉取
- (void)initData {
    parentCompanyModified = NO;
    self.selectedBranchCompany = nil;
    self.selectedAdministrator = nil;
    self.selectedNation = [NSMutableArray new];
    self.selectedProvince = [NSMutableArray new];
    self.selectedCity = [NSMutableArray new];
}

- (void)isUserExist:(NSString*)userName {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    NSString *name = [Tools base64String:userName];
    [dic setObject:name forKey:@"un"];
    
    [HttpClient getWithUrl:HTTP_GET_ISUSEREXIST paramDic:dic resultClass:[CommonResponse class] success:^(CommonResponse *response) {
        if (response.isUserExistResult == 1) {
            //用户存在
            [Tools showMessageCenter:@"当前修改的用户名已被注册，请设置新的用户名。"];
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        else {
            self.mAccount = userName;
        }
    } failure:^(NSError *error) {
        CMLog(@"请求数据失败！，错误信息为%@",error);
    }];
}

- (void)loadUserProfileData {
    [HttpClient getWithUrl:HTTP_GET_USER_PROFILE token:[[AccountManager manager]token] paramDic:nil resultClass:[UserProfileData class] success:^(UserProfileData *response) {
        if (response.getUserProfileResult != nil) {
            self.mUserProfileData = response.getUserProfileResult;
            [self setViewData];
            [self setSelectedData];
            [self.tableView reloadData];
            CMLog(@"请求用户profile成功！");
        }
        
    } failure:^(NSError *error) {
        CMLog(@"请求数据失败！，错误信息为%@",error);
    }];
}

- (void)setSelectedData {
    if (self.mUserProfileData.parentCompany != nil && self.mUserProfileData.parentCompany.length > 0) {
        if (self.mRegisterType == UserTypeBranchAdmin || self.mRegisterType == UserTypeDealer) {
            self.selectedBranchCompany = [UserProfileData new];
            self.selectedBranchCompany.userName = self.mUserProfileData.parentCompanyUserName;
            self.selectedBranchCompany.name = self.mUserProfileData.parentCompany;
        }
        else {
            self.selectedAdministrator = [UserProfileData new];
            self.selectedAdministrator.userName = self.mUserProfileData.parentCompanyUserName;
            self.selectedAdministrator.name = self.mUserProfileData.parentCompany;
        }
    }
    
    if (self.mUserProfileData.operationNations != nil && self.mUserProfileData.operationNations.length > 0) {
        self.selectedNation = [NSMutableArray new];
        NSArray *arrayId = [self.mUserProfileData.operationNations componentsSeparatedByString:@","];
        NSArray *arrayName = [self.mUserProfileData.operationNationNames componentsSeparatedByString:@","];
        NSInteger count = arrayId.count > arrayName.count ? arrayName.count : arrayId.count;
        if (arrayId != nil && arrayName != nil) {
            for (NSInteger i = 0; i < count; i++) {
                //验证数据合法性
                if ([[arrayId objectAtIndex:i] length] != EMPTY_DEVICE_ID.length) {
                    continue;
                }
                ProvinceData *data = [ProvinceData new];
                data.guidId = [arrayId objectAtIndex:i];
                data.name = [arrayName objectAtIndex:i];
                [self.selectedNation addObject:data];
            }
        }
    }
    
    if (self.mUserProfileData.operationProvinces != nil && self.mUserProfileData.operationProvinces.length > 0) {
        self.selectedProvince = [NSMutableArray new];
        NSArray *arrayId = [self.mUserProfileData.operationProvinces componentsSeparatedByString:@","];
        NSArray *arrayName = [self.mUserProfileData.operationProvinceNames componentsSeparatedByString:@","];
        NSInteger count = arrayId.count > arrayName.count ? arrayName.count : arrayId.count;
        if (arrayId != nil && arrayName != nil) {
            for (NSInteger i = 0; i < count; i++) {
                //验证数据合法性
                if ([[arrayId objectAtIndex:i] length] != EMPTY_DEVICE_ID.length) {
                    continue;
                }
                ProvinceData *data = [ProvinceData new];
                data.guidId = [arrayId objectAtIndex:i];
                data.name = [arrayName objectAtIndex:i];
                [self.selectedProvince addObject:data];
            }
        }
    }
    
    if (self.mUserProfileData.operationCities != nil && self.mUserProfileData.operationCities.length > 0) {
        self.selectedCity = [NSMutableArray new];
        NSArray *arrayId = [self.mUserProfileData.operationCities componentsSeparatedByString:@","];
        NSArray *arrayName = [self.mUserProfileData.operationCityNames componentsSeparatedByString:@","];
        NSInteger count = arrayId.count > arrayName.count ? arrayName.count : arrayId.count;
        if (arrayId != nil && arrayName != nil) {
            for (NSInteger i = 0; i < count; i++) {
                //验证数据合法性
                if ([[arrayId objectAtIndex:i] length] != EMPTY_DEVICE_ID.length) {
                    continue;
                }
                ProvinceData *data = [ProvinceData new];
                data.guidId = [arrayId objectAtIndex:i];
                data.name = [arrayName objectAtIndex:i];
                [self.selectedCity addObject:data];
            }
        }
        
    }
    
}

- (void)setViewData {
    if (self.mUserProfileData != nil) {
        
        if ([self.mUserProfileData.role isEqualToString:@"BranchCompany"]) {
            self.mUserLevel = @"暖通公司";
            self.mRegisterType = UserTypeBranchCompany;
        }
        else if ([self.mUserProfileData.role isEqualToString:@"Manager"]) {
            self.mUserLevel = @"大区管理员";
            self.mRegisterType = UserTypeAreaAdmin;
        }
        else if ([self.mUserProfileData.role isEqualToString:@"Administrator"]) {
            self.mUserLevel = @"管理员";
            self.mRegisterType = UserTypeAdministrator;
        }
        else if ([self.mUserProfileData.role isEqualToString:@"BranchManager"]) {
            self.mUserLevel = @"暖通公司零售经理";
            self.mRegisterType = UserTypeBranchAdmin;
        }
        else if ([self.mUserProfileData.role isEqualToString:@"Reseller"]) {
            self.mUserLevel = @"经销商";
            self.mRegisterType = UserTypeDealer;
        }
        else if ([self.mUserProfileData.role isEqualToString:@"Company"]) {
            self.mUserLevel = @"生产厂家";
            self.mRegisterType = UserTypeAdministrator;
        }
        else {
            self.mUserLevel = @"销售人员";
            self.mRegisterType = UserTypeSalePersonal;
        }

        self.mAccount = self.mUserProfileData.userName;
        self.mPassword = @"";
        self.mConfirmPassword = @"";
        
        
        self.mCompanyFullName = self.mUserProfileData.name;
        self.mCompanyContactName = self.mUserProfileData.contactName;
        self.mCompanyContactPhone = self.mUserProfileData.phoneNumber;
        
        self.mBranchCompanyName = self.mUserProfileData.parentCompany;
        
    }
}


#pragma mark - UITableview代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.mRegisterType == UserTypeAdministrator || self.mRegisterType == UserTypeCompany) {
        return 3;
    }
    else if (self.mRegisterType == UserTypeAreaAdmin) {
        return 4;
    }
    return 5;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        //用户名 密码 确认密码
        return 3;
    } else if (section == 2) {
        //用户信息 非销售人员时，需要输入公司或者分销商的名称
        if (self.mRegisterType == UserTypeSalePersonal || self.mRegisterType == UserTypeCompany || self.mRegisterType == UserTypeAdministrator || self.mRegisterType == UserTypeAreaAdmin) {
            return 2;
        }
        else {
            return 3;
        }
    }
    else if (section == 3) {
        return 3;
    }
    //其他情况下，都是只有1个cell
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Register_Cell"];
    
    if (indexPath.section == 0) {
        //邮箱、密码、确认密码
        Icon_Input_Cell *inputCell = [[Icon_Input_Cell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Register_Input_Cell"];
        if (indexPath.row == 0) {
            cell.textLabel.text = self.mUserProfileData.userName;
            return cell;
        }else if (indexPath.row == 1){
            passwordModified = NO;
            [inputCell configPlaceHolder:@"修改密码" andContent:nil andIcon:nil andIsSecure:YES];
            inputCell.textValueChangeBlock = ^(NSString *content){
                self.mPassword = content;
                passwordModified = YES;
            };
//            inputCell.editDidEndBlock = ^(NSString *content){
//                self.mPassword = content;
//                passwordModified = YES;
//            };
        }else if (indexPath.row == 2){
            [inputCell configPlaceHolder:@"再次输入密码" andContent:nil andIcon:nil andIsSecure:YES];
            inputCell.textValueChangeBlock = ^(NSString *content){
                self.mConfirmPassword = content;
                passwordModified = YES;
            };
//            inputCell.editDidEndBlock = ^(NSString *content){
//                self.mConfirmPassword = content;
//            };
        }
        
        return inputCell;
    }else if (indexPath.section == 1){
        cell.textLabel.text = @"用户级别";
        cell.detailTextLabel.text = self.mUserLevel;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else if (indexPath.section == 2) {
        //用户信息 非销售人员时，需要输入公司或者分销商的名称
        Icon_Input_Cell *inputCell = [[Icon_Input_Cell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Register_Info_Cell"];
        if (self.mRegisterType == UserTypeSalePersonal || self.mRegisterType == UserTypeCompany || self.mRegisterType == UserTypeAdministrator || self.mRegisterType == UserTypeAreaAdmin) {
            if (indexPath.row == 0) {
                [inputCell configPlaceHolder:@"修改姓名" andContent:self.mUserProfileData.contactName andIcon:nil andIsSecure:NO];
                
                inputCell.textValueChangeBlock = ^(NSString *content){
                    if (self.mUserProfileData.contactName == nil || ![self.mUserProfileData.contactName isEqualToString:content]) {
                        self.mUserProfileData.contactName = content;
                    }
                };
                
            }else if (indexPath.row == 1){
                [inputCell configPlaceHolder:@"修改手机号" andContent:self.mUserProfileData.phoneNumber andIcon:nil andIsSecure:NO];
                
                inputCell.textValueChangeBlock = ^(NSString *content){
                    if (self.mUserProfileData.phoneNumber == nil || ![self.mUserProfileData.phoneNumber isEqualToString:content]) {
                        self.mUserProfileData.phoneNumber = content;
                    }
                };
            }
        }
        else {
            if (indexPath.row == 1) {
                [inputCell configPlaceHolder:@"修改联系人姓名" andContent:self.mUserProfileData.contactName andIcon:nil andIsSecure:NO];
                
                inputCell.textValueChangeBlock = ^(NSString *content){
                    if (self.mUserProfileData.contactName == nil || ![self.mUserProfileData.contactName isEqualToString:content]) {
                        self.mUserProfileData.contactName = content;
                    }
                };
                
            }else if (indexPath.row == 2){
                [inputCell configPlaceHolder:@"修改手机号" andContent:self.mUserProfileData.phoneNumber andIcon:nil andIsSecure:NO];
                [inputCell configKeyboardType:UIKeyboardTypeNamePhonePad];
                inputCell.textValueChangeBlock = ^(NSString *content){
                    if (self.mUserProfileData.phoneNumber == nil || ![self.mUserProfileData.phoneNumber isEqualToString:content]) {
                        self.mUserProfileData.phoneNumber = content;
                    }
                };
            }
            else {
                if (self.mRegisterType == UserTypeBranchCompany) {
                    [inputCell configPlaceHolder:@"修改暖通公司名称" andContent:self.mUserProfileData.name andIcon:nil andIsSecure:NO];
                }
                else {
                    [inputCell configPlaceHolder:@"修改经销商名称" andContent:self.mUserProfileData.name andIcon:nil andIsSecure:NO];
                }
                
                inputCell.textValueChangeBlock = ^(NSString *content){
                    if (self.mUserProfileData.name == nil || ![self.mUserProfileData.name isEqualToString:content]) {
                        self.mUserProfileData.name = content;
                    }
                };
            }
            
            
            
        }
        return inputCell;
    }
    else if (indexPath.section == 3) {
            //经营区域
            if (indexPath.row == 0) {
                cell.textLabel.text = @"修改经营国家";
                if (self.selectedNation && self.selectedNation.count > 0) {
                    NSMutableString *detail = [NSMutableString new];
                    for (ProvinceData *item in self.selectedNation) {
                        if (item.name != nil) {
                            [detail appendString:item.name];
                            [detail appendString:@" "];
                        }
                    }
                    cell.detailTextLabel.text = detail;
                }
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
            else if (indexPath.row == 1) {
                cell.textLabel.text = @"修改经营省份";
                if (self.selectedProvince && self.selectedProvince.count > 0) {
                    NSMutableString *detail = [NSMutableString new];
                    for (ProvinceData *item in self.selectedProvince) {
                        if (item.name != nil) {
                            [detail appendString:item.name];
                            [detail appendString:@" "];
                        }
                    }
                    cell.detailTextLabel.text = detail;
                }
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
            else {
                cell.textLabel.text = @"修改经营城市";
                if (self.selectedCity && self.selectedCity.count > 0) {
                    NSMutableString *detail = [NSMutableString new];
                    for (ProvinceData *item in self.selectedCity) {
                        if (item.name != nil) {
                            [detail appendString:item.name];
                            [detail appendString:@" "];
                        }
                    }
                    cell.detailTextLabel.text = detail;
                }
                
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
    }
    else {
        if (self.mRegisterType == UserTypeBranchCompany) {
            cell.textLabel.text = @"选择所属大区管理员";
        }
        else if (self.mRegisterType == UserTypeSalePersonal) {
            cell.textLabel.text = @"修改所属暖通公司或者经销商";
        }
        else {
            cell.textLabel.text = @"修改所属暖通公司";
        }
        
        if (self.mRegisterType == UserTypeBranchCompany) {
            //设置大区管理员信息
            if (self.selectedAdministrator != nil) {
                cell.detailTextLabel.text = self.selectedAdministrator.name;
            }
        }
        else {
            //上级公司信息
            if (self.selectedBranchCompany != nil) {
                cell.detailTextLabel.text = self.selectedBranchCompany.name;
            }
            else if ((NSNull *)self.mBranchCompanyName != [NSNull null]) {
                cell.detailTextLabel.text = self.mBranchCompanyName;
            }
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.section) {
        case 0:
            
            break;
        case 1:{
//            UIActionSheet *sheet = [UIActionSheet bk_actionSheetWithTitle:@"选择用户级别"];
//            [sheet bk_addButtonWithTitle:@"暖通公司" handler:^{
//                self.mUserLevel = @"暖通公司";
//                self.mRegisterType = RegisterTypeCompany;
//                [tableView reloadData];
//            }];
//            [sheet bk_addButtonWithTitle:@"经销商" handler:^{
//                self.mUserLevel = @"经销商";
//                self.mRegisterType = RegisterTypeDealer;
//                [tableView reloadData];
//            }];
//            [sheet bk_addButtonWithTitle:@"销售人员" handler:^{
//                self.mUserLevel = @"销售人员";
//                self.mRegisterType = RegisterTypeSalePersonal;
//                [tableView reloadData];
//            }];
//            [sheet bk_setCancelButtonWithTitle:@"取消" handler:nil];
//            [sheet showInView:self.view];
            break;
        }
        case 2:
            break;
        case 3:{
            if (indexPath.row == 0) {
                //选择国家
                SelectionNationViewController *view = [SelectionNationViewController new];
                view.selectedData = [NSMutableArray arrayWithArray:self.selectedNation];
                view.couldMultipleSelect = YES;
                view.userSelectedBlock = ^(NSMutableArray *selectedData) {
                    if (self.selectedNation == nil) {
                        self.selectedNation = [NSMutableArray new];
                    }
                    if (selectedData.count > 0) {
                        [self.selectedNation removeAllObjects];
                        [self.selectedNation addObjectsFromArray:selectedData];
                        [self.tableView reloadData];
                        
                    }
                };
                [self.navigationController pushViewController:view animated:YES];
                
            }
            else if (indexPath.row == 1) {
                //选择省份
                if (self.selectedNation == nil || self.selectedNation.count == 0) {
                    kTipAlert(@"请先选择国家");
                    return;
                }
                SelectProvinceViewController *view = [SelectProvinceViewController new];
                view.selectedNation = [NSMutableArray arrayWithArray:self.selectedNation];
                view.selectedData = [NSMutableArray arrayWithArray:self.selectedProvince];
                view.couldMultipleSelect = YES;
                view.userSelectedBlock = ^(NSMutableArray *selectedData) {
                    if (self.selectedProvince == nil) {
                        self.selectedProvince = [NSMutableArray new];
                    }
                    if (selectedData.count > 0) {
                        if (self.selectedCity != nil) {
                            [self.selectedCity removeAllObjects];
                        }
                        [self.selectedProvince removeAllObjects];
                        [self.selectedProvince addObjectsFromArray:selectedData];
                        [self.tableView reloadData];
                        
                    }
                };
                [self.navigationController pushViewController:view animated:YES];
            }
            else {
                //选择城市
                if (self.selectedProvince == nil || self.selectedProvince.count == 0) {
                    kTipAlert(@"请先选择省份");
                    return;
                }
                SelectCityViewController *view = [SelectCityViewController new];
                view.selectedProvince = [NSMutableArray arrayWithArray:self.selectedProvince];
                view.selectedData = [NSMutableArray arrayWithArray:self.selectedCity];
                view.couldMultipleSelect = YES;
                view.userSelectedBlock = ^(NSMutableArray *selectedData) {
                    if (self.selectedCity == nil) {
                        self.selectedCity = [NSMutableArray new];
                    }
                    if (selectedData.count > 0) {
                        [self.selectedCity removeAllObjects];
                        [self.selectedCity addObjectsFromArray:selectedData];
                        [self.tableView reloadData];
                        
                    }
                };
                [self.navigationController pushViewController:view animated:YES];
            }
            break;
        }
        case 4:{
            
            
            break;
        }
        default:
            break;
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
